package com.sxkf.template.service.impl;

import cn.hutool.core.date.DateUtil;
import com.sxkf.core.constant.CommonConstant;
import com.sxkf.template.entity.Coupon;
import com.sxkf.template.entity.UserCoupon;
import com.sxkf.template.mapper.CouponMapper;
import com.sxkf.template.mapper.UserCouponMapper;
import com.sxkf.template.service.UserCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 用户优惠卷 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-02 18:10:34
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {
    @Resource
    private CouponMapper couponMapper;
    /**
     * 用户领取优惠券
     *
     * @param userId   用户id
     * @param couponId 优惠券id
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void userReceiveCoupon(String userId, String couponId) {
        Coupon coupon = couponMapper.selectById(couponId);
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setUserId(userId);
        userCoupon.setCouponId(couponId);
        userCoupon.setStoreId(coupon.getStoreId());
        userCoupon.setCloseTime(DateUtil.offsetDay(new Date(),coupon.getValidTime()));
        userCoupon.setState(CommonConstant.ENABLE);
        userCoupon.setCouponPrice(coupon.getPrice());
        userCoupon.setCouponName(coupon.getName());
        userCoupon.setCouponMinPrice(coupon.getMinPrice());
        userCoupon.setIsDelete("0");
        save(userCoupon);
    }
}
